package com.justnow.offer;

/**
 * @author justnow
 * Created on 2020-08-30
 * Description
 * 判断一个整数是否是回文数。回文数是指正序（从左到右）和倒序（从右到左）读都是一样的整数
 */
public class Solution15_1 {

    /**
     * 方法一，转换为字符串，然后字符串取逆
     * @param x
     * @return
     */
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        String o = String.valueOf(x);
        String r = new StringBuffer(o).reverse().toString();
        int length = o.length();
        for (int i = 0; i < length; i++) {

            if (o.charAt(i) != r.charAt(i)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 方法二，求其数字的取反之后的值，然后进行比对
     * @param x
     * @return
     */
    public boolean isPalindrome2(int x) {
        if (x < 0)
            return false;
        int temp = x;
        int cur = 0;
        while (x > 0) {
            cur = cur * 10 + x % 10;
            x /= 10;
        }

        if (temp != cur)
            return false;

        return true;

    }
    public static void main(String[] args) {
        Solution15_1 solution15_1 = new Solution15_1();
        boolean palindrome = solution15_1.isPalindrome(10);
        System.out.println(palindrome);
    }
}
